package org.jeecg.modules.reserve.utils;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

/**
 * @ClassName: QRCodeGenerator
 * @Author: hzliu
 * @Date: 2020-05-13 13:05
 **/
public class QRCodeGenerator {

    /**
     * 二维码生成
     *
     * @param text 二维码内容
     * @param width 宽度
     * @param height 高度
     * @param filePath 二维码存储地址
     * @throws WriterException
     * @throws IOException
     */
    public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }


    public static void main(String[] args) {
        try {
            String qrCode = "百度.png";
            generateQRCodeImage("http://www.baidu.com",350, 350, "d://WorkSpaces//upload//" + qrCode);
        } catch (IOException | WriterException e) {
            e.printStackTrace();
        }
    }
}